package edu.xidian.bdilab.jrbigdata.service.government.creditRating.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.xidian.bdilab.jrbigdata.controller.bank.BankCode;
import edu.xidian.bdilab.jrbigdata.controller.enterprise.EnterpriseCode;
import edu.xidian.bdilab.jrbigdata.controller.response.ResponseResult;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseCreditRate;
import edu.xidian.bdilab.jrbigdata.domain.EnterpriseInfo;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseCreditRateMapper;
import edu.xidian.bdilab.jrbigdata.mapper.EnterpriseInfoMapper;
import edu.xidian.bdilab.jrbigdata.service.government.creditRating.CreditingRatingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Boolean.FALSE;
import static java.lang.Boolean.TRUE;

/**
 * @ClassName creditingRatingServiceImpl
 * @Description TODO
 * @Author tuantuan
 * @Date 2019/4/14 下午8:10
 * @Version 1.0
 * @Attention Copyright (C), 2004-2019, BDILab, XiDian University
 **/
@Service
public class CreditingRatingServiceImpl implements CreditingRatingService{
    @Autowired
    EnterpriseInfoMapper enterpriseInfoMapper;

    @Autowired
    EnterpriseCreditRateMapper enterpriseCreditRateMapper;
    @Override
    public ResponseResult getUnratedEnterprise(int pageNum,int pageSize) throws Exception{
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(pageNum, pageSize);
        EnterpriseInfo enterpriseInfo;
        enterpriseInfoQueryWrapper.eq("is_credit_rate","0");
        try{
            List<EnterpriseInfo> enterpriseInfoList=enterpriseInfoMapper.selectList(enterpriseInfoQueryWrapper);
            PageInfo pageInfo = new PageInfo<>(enterpriseInfoList);
            ResponseResult responseResult = new ResponseResult();
            Map<String, Object> data = new HashMap<>(3);
            data.put("未被评级的企业列表", enterpriseInfoList);
            data.put("Total Page Num", pageInfo.getPages());
            data.put("Total", pageInfo.getTotal());
            if (enterpriseInfoList.size() == 0) {
                responseResult.setCode(BankCode.CHECK_NONE.getCode());
                responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
                responseResult.setSuccess(TRUE);
            } else {
                responseResult.setData(data);
                responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
                responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
                responseResult.setSuccess(TRUE);
            }
            return responseResult;
        }catch(Exception e){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
            responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
            responseResult.setSuccess(FALSE);
            return responseResult;
        }
    }

    @Override
    public  ResponseResult rateEnterprise(String userName,int creditRate)throws Exception{
        EnterpriseCreditRate enterpriseCreditRate=new EnterpriseCreditRate();
        ResponseResult responseResult=new ResponseResult();
        try{
            //判断是否存在该企业用户
            QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper = new QueryWrapper<>();
            enterpriseInfoQueryWrapper.eq("social_code", userName);
            EnterpriseInfo enterpriseInfo=enterpriseInfoMapper.selectOne(enterpriseInfoQueryWrapper);
            if(enterpriseInfo!=null){
                enterpriseCreditRate.setCreditRate(creditRate);
                enterpriseCreditRate.setUserName(userName);
                if(enterpriseInfo.getIsCreditRate()==1){
                    enterpriseCreditRateMapper.updateById(enterpriseCreditRate);
                }else{
                    enterpriseInfo.setIsCreditRate(1);
                    enterpriseInfoMapper.update(enterpriseInfo,enterpriseInfoQueryWrapper);
                    enterpriseCreditRateMapper.insert(enterpriseCreditRate);
                }

            }
            else{
                responseResult.setCode(EnterpriseCode.RATE_FAILED.getCode());
                responseResult.setMessage("该用户名不存在");
                responseResult.setSuccess(false);
                return responseResult;
            }
            responseResult.setCode(EnterpriseCode.RATE_SUCCESS.getCode());
            responseResult.setMessage(EnterpriseCode.RATE_SUCCESS.getMessage());
            responseResult.setSuccess(true);
            return responseResult;
        }catch (Exception e){
            responseResult.setCode(EnterpriseCode.RATE_FAILED.getCode());
            responseResult.setMessage(EnterpriseCode.RATE_FAILED.getMessage());
            responseResult.setSuccess(false);
            return responseResult;
        }
    }

    @Override
    public  ResponseResult getRatedEnterprise(int pageNum,int pageSize)throws Exception{
        QueryWrapper<EnterpriseInfo> enterpriseInfoQueryWrapper=new QueryWrapper<>();
        PageHelper.startPage(pageNum, pageSize);
        EnterpriseInfo enterpriseInfo;
        enterpriseInfoQueryWrapper.eq("is_credit_rate","1");
        try{
            List<EnterpriseInfo> enterpriseInfoList=enterpriseInfoMapper.selectList(enterpriseInfoQueryWrapper);
            List<Object> list=new ArrayList<>();
            ResponseResult responseResult = new ResponseResult();
            if(enterpriseInfoList.size()==0){
                responseResult.setCode(BankCode.CHECK_NONE.getCode());
                responseResult.setMessage(BankCode.CHECK_NONE.getMessage());
                responseResult.setSuccess(TRUE);
            }else{
                for(EnterpriseInfo enterpriseInfo1:enterpriseInfoList){
                    EnterpriseCreditRate enterpriseCreditRate=enterpriseCreditRateMapper.selectById(enterpriseInfo1.getSocialCode());
                    Map<String, Object> map = new HashMap<>(2);
                    map.put("EnterpriseInfo", enterpriseInfo1);
                    map.put("CreditRate",enterpriseCreditRate.getCreditRate());
                    list.add(map);
                }
                PageInfo pageInfo = new PageInfo<>(enterpriseInfoList);
                Map<String, Object> data = new HashMap<>(3);
                data.put("已被评级的企业列表", list);
                data.put("Total Page Num", pageInfo.getPages());
                data.put("Total", pageInfo.getTotal());
                responseResult.setData(data);
                responseResult.setCode(BankCode.CHECK_SUCCESS.getCode());
                responseResult.setMessage(BankCode.CHECK_SUCCESS.getMessage());
                responseResult.setSuccess(TRUE);
            }
            return responseResult;
        }catch(Exception e){
            ResponseResult responseResult = new ResponseResult();
            responseResult.setCode(BankCode.CHENCK_FAILED.getCode());
            responseResult.setMessage(BankCode.CHENCK_FAILED.getMessage());
            responseResult.setSuccess(FALSE);
            return responseResult;
        }
    }

}
